<?php
/**
 * Created by PhpStorm.
 * User: 毛文科
 * Date: 2018/8/23 0023
 * Time: 11:28
 */
namespace app\admin\model;
use think\Db;

class DepartmenModel
{
    /**获取部门模型
     * @return $this
     */
    static public function getDepartmen(){
        return Db::view('departmen_table',true)
            ->view('company','company_name','company.id=departmen_table.company_id','left');
    }

    /**添加部门
     * @param $data
     * @return int|string
     */
    static public function addDepartmen($data){
        return Db::name('departmen_table')->insert($data);
    }

    /**删除部门
     * @param $field
     * @param null $op
     * @param null $condition
     * @return int
     */
    static public function delDepartmen($field, $op = null, $condition = null){
        $sul = Db::connect();
        $datas = $sul->name('departmen_table')->where($field,$op,$condition)->select();
        foreach ($datas as $data){
            $id = $data['id'];
            $sul->name('position_table')->where('department_id',$id)->delete();
            $sul->name('card_setting')->where('department_id',$id)->delete();
            $sul->name('leave')->where('department_id',$id)->delete();
            $sul->name('punch_clock')->where('department_id',$id)->delete();
            $user = $sul->name('user')->where('department_id',$id)->select();
            foreach ($user as $value){
                if($value['portrait']){
                    if(is_file(config('FileRoot').$value['portrait'])){
                        unlink(config('FileRoot').$value['portrait']);
                    }
                }
            }
            $sul->name('user')->where('department_id',$id)->delete();
            $sul->name('work_report')->where('department_id',$id)->delete();
            $sul->name('integral_change')->where('department_id',$id)->delete();
            $sul->name('post_release')->where('department_id',$id)->delete();
            $sul->name('comment')->where('department_id',$id)->delete();
        }
        return $sul->name('departmen_table')->where($field,$op,$condition)->delete();
    }

    /**更新部门
     * @param $data
     * @return int|string
     */
    static public function updateDepartmen($data){
        return Db::name('departmen_table')->update($data);
    }
}